'use strict'

$(function(){
    query()
})

function render(arr){
    let row = $('.rowData')
    let tb = $('#tbData')
    row.remove()
    arr.forEach(item => {
        let html=`
        <tr class="rowData" key="${item.id}">
            <td>${item.id}</td>
            <td>${item.tite}</td>
            <td>${item.imports}</td>
            <td>${item.text}</td>
            <td>${item.cat}</td>
            <td>${item.atou}</td>
            <td>${item.time}</td>
            <td>
                <input type="button" value="编辑" onclick="Edit(${item.id})">
                <input type="button" value="删除" onclick="Del(${item.id})">
            </td>
        </tr>
        `
        tb.append(html)
    });
}

function query(){
    let keyword = $('#keyword').val()
    console.log(keyword);
    getBlog(keyword).then(item=>{
        render(item)
    })
}

function add(){
    location.href='./addOrEdit.html'
}

function Edit(id){
    location.href = `./addOrEdit.html?${id}`
}

function Del(id){
    let confirmDel = confirm(`确认删除id为${id}的数据吗？`)
    if(confirmDel){
        delBlog(id).then(data=>{
            let id = data.data.id
            $(`[key=${id}]`).remove()
        }).then(
            location.href = './index.html'
        )
        
    }
}